package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.DeInfoExcelEx;
import com.be.beadmin.device.dto.output.SpareInfoEx;
import com.be.beadmin.device.dto.output.SpareInfoExcelEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.SpareInfoService;
import com.be.beadmin.device.dto.input.SpareInfoDto;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-07-30
**/
@RestController
public class SpareInfoController {

    @Autowired
    private  SpareInfoService spareInfoService;


    @PostMapping(value = "/device/getSpareInfoByPage")
    public ResultModel getSpareInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(spareInfoService.getSpareInfoByPage(pageEntity)) ;
    }

    @PostMapping("/device/addSpareInfo")
    public ResultModel addSpareInfo(@RequestBody SpareInfoDto spareInfoDto){
        spareInfoService.addSpareInfo(spareInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delSpareInfo")
    public ResultModel delSpareInfo(@RequestBody SpareInfoDto spareInfoDto){
        spareInfoService.delSpareInfo(spareInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateSpareInfo")
    public ResultModel updateSpareInfo(@RequestBody SpareInfoDto spareInfoDto){
        spareInfoService.updateSpareInfo(spareInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportSpareInfo")
    public void exportSpareInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<SpareInfoEx> spareInfoPage = spareInfoService.getSpareInfoByPage(pageEntity);
        List<SpareInfoEx> spareInfoExes = spareInfoPage.getContent();
        List<SpareInfoExcelEx> spareInfoExcelExes=spareInfoExes.stream().map(item->{
            SpareInfoExcelEx excel=new SpareInfoExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("SpareInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), SpareInfoExcelEx.class).sheet("sheet1").doWrite(spareInfoExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), SpareInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(spareInfoExcelExes);
    }

    // 筛选功能
    @ApiOperation("根据仓库名称获取项目信息")
    @PostMapping(value = "/project/getSpareInfoByWarehouseNameAndContractDate")
    public ResultModel getSpareInfoByWarehouseNameAndContractDate(@RequestParam String warehouseName, @RequestParam Date contractDate) {
        return ResultModel.success(spareInfoService.getSpareInfoByWarehouseNameAndContractDate(warehouseName, contractDate));
    }

}